<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 用药章节 (4 《西药处方》、 5 《中药处方》)-->
	<xsl:template match="*" mode="HistoryOfMedicationUseSection">
		<xsl:choose>
			<xsl:when test="($hccDocType='C0004') or ($hccDocType='C0005')">
				<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HistoryOfMedicationUse]" mode="HistoryOfMedicationUseEntry">
				</xsl:apply-templates>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	<!-- 用药章节条目 -->
	<xsl:template match="*" mode="HistoryOfMedicationUseEntry">
		<xsl:apply-templates select="hl7:entry/hl7:substanceAdministration" mode="HCCMedication">
			<xsl:with-param name="sectionType" select="'HMU'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 用药管理章节 -->
	<xsl:template match="*" mode="MedicationAdministeredSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_MedicationAdministered]" mode="MedicationAdministeredEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 用药管理章节条目 -->
	<xsl:template match="*" mode="MedicationAdministeredEntry">
		<xsl:apply-templates select="hl7:entry" mode="HCCMedication">
			<xsl:with-param name="sectionType" select="'MA'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!-- 医嘱章节 -->
	<xsl:template match="*" mode="ProviderMedOrdersSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_ProviderOrders]" mode="ProviderMedOrdersEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 医嘱章节条目 (仅处理药嘱条目) -->
	<xsl:template match="*" mode="ProviderMedOrdersEntry">
		<xsl:apply-templates select="hl7:entry/hl7:organizer[(hl7:component/hl7:observation/hl7:code/@code='DE06.00.289.00') and (hl7:component/hl7:observation/hl7:value/@code='01')]" mode="HCCMedication">
			<xsl:with-param name="sectionType" select="'PMO'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 卫生事件章节 -->
	<xsl:template match="*" mode="HealthcareEventMedSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_HealthcareEvent]" mode="HealthcareEventMedEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 卫生事件章节条目 (仅处理关键药物名称条目) -->
	<xsl:template match="*" mode="HealthcareEventMedEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation[hl7:code/@code='DE08.50.022.00']" mode="HCCMedication">
			<xsl:with-param name="sectionType" select="'HEM'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!--通用药嘱条目输出模板-->
	<xsl:template match="*" mode="HCCMedication">
		<xsl:param name = "sectionType" />
		<Medication>
		<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
		<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
		<xsl:choose>
		<!--用药章节-->
		<xsl:when test="$sectionType='HMU'">
			<OrderItem>
				<Code><xsl:value-of select="hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:code"/></Code>
				<Description><xsl:value-of select="hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></Description>
			</OrderItem>
			<!--
			<DrugProduct>
				<Code><xsl:value-of select="hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:code"/></Code>
				<Description><xsl:value-of select="hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></Description>
				<ProductName><xsl:value-of select="hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></ProductName>
				<PackageSizeUnits>
					<Code><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.043.00']/hl7:value"/></Code>
					<Description><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.043.00']/hl7:value"/></Description>					
				</PackageSizeUnits>
			</DrugProduct>-->
			<OrderQuantity><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.135.00']/hl7:value/@value"/></OrderQuantity>
			<Route>
				<Code><xsl:value-of select="hl7:routeCode/@code"/></Code>
				<Description><xsl:value-of select="hl7:routeCode/@displayName"/></Description>
			</Route>
			<DosageForm>
				<Code><xsl:value-of select="hl7:administrationUnitCode/@code"/></Code>
				<Description><xsl:value-of select="hl7:administrationUnitCode/@displayName"/></Description>
			</DosageForm>	
			<DoseQuantity><xsl:value-of select="hl7:doseQuantity/@value"/></DoseQuantity>
			<DoseUoM>
				<Code><xsl:value-of select="hl7:doseQuantity/@unit"/></Code>
				<Description><xsl:value-of select="hl7:doseQuantity/@unit"/></Description>
			</DoseUoM>							
			<Duration>
				<Code><xsl:value-of select="../../hl7:entry/hl7:observation[hl7:code/@code='DE06.00.294.00']/hl7:value/@unit"/></Code>
				<Description><xsl:value-of select="../../hl7:entry/hl7:observation[hl7:code/@code='DE06.00.294.00']/hl7:value/@unit"/></Description>
				<Factor><xsl:value-of select="../../hl7:entry/hl7:observation[hl7:code/@code='DE06.00.294.00']/hl7:value/@value"/></Factor>
			</Duration>
			<!--
			<PrescriptionNumber><xsl:value-of select="../../hl7:entry/hl7:observation[hl7:code/@code='DE08.50.056.00']/hl7:value/@value"/>-->
			<PrescriptionNumber><xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.20']/@extension"/>
			</PrescriptionNumber>
		</xsl:when>
		
		<!--用药管理章节-->
		<xsl:when test="$sectionType='MA'">
			<xsl:variable name="drugCode" select="hl7:substanceAdministration/hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:code" />
			<OrderItem>
				<xsl:choose>
				<xsl:when test="string-length($drugCode)">
					<Code><xsl:value-of select="$drugCode"/></Code>
				</xsl:when>
				<xsl:otherwise>
					<Code><xsl:value-of select="hl7:substanceAdministration/hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></Code>
				</xsl:otherwise>
				</xsl:choose>
				<Description><xsl:value-of select="hl7:substanceAdministration/hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></Description>
			</OrderItem>
			<Route>
				<Code><xsl:value-of select="hl7:substanceAdministration/hl7:routeCode/@code"/></Code>
				<Description><xsl:value-of select="hl7:substanceAdministration/hl7:routeCode/@displayName"/></Description>
			</Route>
			<DoseQuantity><xsl:value-of select="hl7:substanceAdministration/hl7:doseQuantity/@value"/></DoseQuantity>
			<TextInstruction>
				<xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.136.00']/hl7:value"/>
			</TextInstruction>
			<!--<DrugProduct>
				<xsl:choose>
					<xsl:when test="string-length($drugCode)">
						<Code><xsl:value-of select="$drugCode"/></Code>
					</xsl:when>
					<xsl:otherwise>
						<Code><xsl:value-of select="hl7:substanceAdministration/hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></Code>
					</xsl:otherwise>
				</xsl:choose>
				<Description><xsl:value-of select="hl7:substanceAdministration/hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></Description>
				<ProductName><xsl:value-of select="hl7:substanceAdministration/hl7:consumable/hl7:manufacturedProduct/hl7:manufacturedLabeledDrug/hl7:name"/></ProductName>
			</DrugProduct>-->
			<OrderQuantity><xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.135.00']/hl7:value/@value"/></OrderQuantity>
			<DosageForm>
				<Code><xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.011.00']/hl7:value/@code"/></Code>
				<Description><xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.011.00']/hl7:value/@displayName"/></Description>
			</DosageForm>				
			<DoseUoM>
				<Code><xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.024.00']/hl7:value"/></Code>
				<Description><xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.024.00']/hl7:value"/></Description>
			</DoseUoM>				
			<Frequency>
				<Code><xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.133.00']/hl7:value/@code"/></Code>
				<Description><xsl:value-of select="hl7:substanceAdministration/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.133.00']/hl7:value/@displayName"/></Description>
			</Frequency>
		</xsl:when>

		<!--医嘱章节-->
		<xsl:when test="$sectionType='PMO'">
			<OrderItem>
				<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Code>
				<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Description>
			</OrderItem>
			<!--
			<DrugProduct>
				<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Code>
				<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></Description>
				<ProductName><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:value"/></ProductName>
			</DrugProduct>-->
			<FromTime><xsl:value-of select="isc:evaluate('xmltimestamp',hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:effectiveTime/hl7:low/@value)"/>
			</FromTime>
			<ToTime><xsl:value-of select="isc:evaluate('xmltimestamp',hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:effectiveTime/hl7:high/@value)"/>
			</ToTime>
			<PrescriptionNumber>
				<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE08.50.056.00']/hl7:value"/>
			</PrescriptionNumber>
			
			<TextInstruction>
				<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.179.00']/hl7:value"/>
			</TextInstruction>
			
			<Status>
				<xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:value"/>
			</Status>
			
			<OrderedBy>
				<xsl:choose>
					<xsl:when test="string-length(hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:id/@extension)">
						<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:id/@extension"/></Code>
					</xsl:when>
					<xsl:otherwise>
						<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:assignedPerson/hl7:name"/></Code>
					</xsl:otherwise>
				</xsl:choose>
				<Name><GivenName><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:author/hl7:assignedAuthor/hl7:assignedPerson/hl7:name"/></GivenName></Name>
			</OrderedBy>
			
			<!--decide if there is a administration record-->
			<xsl:variable name="performTime" select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:performer/hl7:time/@value" />
			<xsl:choose>
				<xsl:when test="string-length($performTime)">
				<Administrations>
					<Administration>
						<AdministeringProvider>
							<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:performer/hl7:assignedEntity/hl7:id/@extension"/></Code>
							<Name><GivenName><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:performer/hl7:assignedEntity/hl7:assignedPerson/hl7:name"/></GivenName></Name>
						</AdministeringProvider>
						<FromTime><xsl:value-of select="isc:evaluate('xmltimestamp',$performTime)"/>
						</FromTime>
						<ToTime><xsl:value-of select="isc:evaluate('xmltimestamp',$performTime)"/>
						</ToTime>
						<AdministeredAtLocation>
							<Code><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:performer/hl7:assignedEntity/hl7:id/@extension"/></Code>
							<Description><xsl:value-of select="hl7:component/hl7:observation[hl7:code/@code='DE06.00.288.00']/hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.290.00']/hl7:performer/hl7:assignedEntity/hl7:representedOrganization/hl7:name"/></Description>
						</AdministeredAtLocation>
					</Administration>
				</Administrations>
				</xsl:when>
			</xsl:choose>
		</xsl:when>		
		
		<!--卫生事件章节-->
		<xsl:when test="$sectionType='HEM'">
			<OrderItem>
				<Code><xsl:value-of select="hl7:value"/></Code>
				<Description><xsl:value-of select="hl7:value"/></Description>
			</OrderItem>
			<!--
			<DrugProduct>
				<Code><xsl:value-of select="hl7:value"/></Code>
				<Description><xsl:value-of select="hl7:value"/></Description>
				<ProductName><xsl:value-of select="hl7:value"/></ProductName>
			</DrugProduct>-->
			<Route>
				<Code><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.136.00']/hl7:value"/></Code>
				<Description><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE06.00.136.00']/hl7:value"/></Description>
			</Route>
		</xsl:when>

		</xsl:choose>
		</Medication>		
	</xsl:template>
	
</xsl:stylesheet>
